package com.lb.shopguide.ui.fragment.guide.order;

import android.annotation.SuppressLint;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.ui.view.NormalTitleBar;

import butterknife.BindView;

/**
 * Created by kang on 2017/6/12.
 */

public class FragmentCheckDelivery extends BaseCommonFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.webview)
    WebView mWebView;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;

    private String webUrl;

    public static FragmentCheckDelivery newInstance(String url) {
        FragmentCheckDelivery fragmentCheckDelivery = new FragmentCheckDelivery();
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.WEB_URL, url);
        fragmentCheckDelivery.setArguments(bundle);
        return fragmentCheckDelivery;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_webview;
    }

    @Override
    public void initData() {
        mUserConfigManager = UserConfigManager.getInstance(mContext);
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.WEB_URL)) {
            webUrl = bundle.getString(AppConstant.WEB_URL);
        }
//        EventBus.getDefault().register(this);
    }

    @SuppressLint("JavascriptInterface")
    @Override
    public void initView() {
        ntb.setTitleText("查看物流");
        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
        // 进度条控制
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (isVisible()) {
                    if (newProgress >= 100) {
                        progressBar.setVisibility(View.GONE);
                    } else {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                    super.onProgressChanged(view, newProgress);
                }
            }

        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
//        mWebView.loadUrl("http://192.168.1.64:8088/v2/editGoods/new");
        mWebView.loadUrl(webUrl);
//        mWebView.loadUrl("http://singlepage.storeer.com/receiveInvite.html?inviteCode=aeb1f239-7c3c-4632-b8e0-28b2aaeab235&trueName=马小宝");
        mWebView.addJavascriptInterface(FragmentCheckDelivery.this, "android");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @Override
    public String getPageTag() {
        return FragmentCheckDelivery.class.getName();
    }

//    @Override
//    public void onDestroyView() {
//        EventBus.getDefault().unregister(this);
//        super.onDestroyView();
//    }

}
